﻿using SistemaAdministracion.Models.BaseClass;
using SistemaAdministracion.Models.PatternBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace SistemaAdministracion.Models.Authentication
{
    public class CustomMembershipProvider : MembershipProvider
    {
        IDbContext _context;
        UnitOfWork _UoW;
        IRepository<Usuario> _reposit;
        IRepository<UsuarioRol> _repUserRole;
        IRepository<Role> _repRole;

        public CustomMembershipProvider()
        {
            this._context = new DbContextAdapter(ContextBase.ContextBaseInstance);
            this._UoW = new UnitOfWork(_context);
            this._reposit = new BaseRepository<Usuario>(_context);
            this._repRole = new BaseRepository<Role>(_context);
            this._repUserRole = new BaseRepository<UsuarioRol>(_context);
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {

            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);
            OnValidatingPassword(args);

            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (RequiresUniqueEmail && GetUserNameByEmail(email) != string.Empty)
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            MembershipUser mUser = GetUser(username, true);

            if (mUser == null)
            {

                Usuario us = new Usuario();
                us.UserName = username;
                us.Password = password;
                us.Email = email;
                _reposit.Add(us);
                _UoW.Commint();

                status = MembershipCreateStatus.Success;
                return GetUser(username, true);
            }
            else
            {
                status = MembershipCreateStatus.DuplicateUserName;
            }
            return null;
        }


        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            Usuario u = _reposit.SingleOrDefault(a => a.UserName == username);
            if (u != null)
            {
                MembershipUser user = new MembershipUser("CustomMembershipProvider",
                                                          u.UserName, u.IdUser, u.Email,
                                                          string.Empty, string.Empty, true, false,
                                                          DateTime.Now.Date, DateTime.MinValue, DateTime.MinValue,
                                                          DateTime.Now.Date, DateTime.Now.Date);
                return user;
            }
            return null;
        }

        public override string GetUserNameByEmail(string email)
        {
            Usuario dato = _reposit.SingleOrDefault(e => e.Email == email);
            if (dato != null)
            {
                return dato.UserName;
            }
            return null;
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return 3; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 5; }
        }

        public override bool ValidateUser(string username, string password)
        {
            Usuario us = _reposit.SingleOrDefault(u => u.UserName == username && u.Password == password);
            if (us != null)
            {
                return true;
            }
            return false;
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { return false; }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

    }
}